/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class TryLayupCalculate {

    public static void main(String[] args) {
        try {
            // 打开材料库和铺层库文件
            FileInputStream matLibFile = new FileInputStream("D:\\code\\isfd-back\\comacins-modules\\comacins-isfd\\src\\main\\java\\com\\comac\\ins\\isfd\\util\\CR929_Material_Lib.xlsm");
            FileInputStream layupLibFile = new FileInputStream("D:\\code\\isfd-back\\comacins-modules\\comacins-isfd\\src\\main\\java\\com\\comac\\ins\\isfd\\util\\CR929_Layup_Lib.xlsm");

            Workbook matLibWorkbook = new XSSFWorkbook(matLibFile);
            Workbook layupLibWorkbook = new XSSFWorkbook(layupLibFile);

            Sheet compositeSheet = matLibWorkbook.getSheet("复合材料");
            Sheet metalSheet = matLibWorkbook.getSheet("金属材料");
            Sheet skinPropertiesSheet = layupLibWorkbook.getSheet("蒙皮属性库");
            Sheet resultSheet = layupLibWorkbook.getSheet("对应等效材料");

            // 读取复合材料属性到字典
            Map<String, double[]> matMap = new HashMap<>();
//            for (int i = 1; i <= compositeSheet.getLastRowNum(); i++) {
//                Row row = compositeSheet.getRow(i);
//                Double matID = row.getCell(1).getNumericCellValue();
//                double[] properties = new double[row.getLastCellNum() - 2];
//                for (int j = 2; j < row.getLastCellNum(); j++) {
//                    System.out.println(row.getCell(j));
//                    properties[j - 2] = row.getCell(j).getNumericCellValue();
//                }
//                matMap.put(matID.toString(), properties);
//            }

            // 读取金属材料属性到字典
            Map<String, double[]> metalMap = new HashMap<>();
            for (int i = 1; i <= metalSheet.getLastRowNum(); i++) {
                Row row = metalSheet.getRow(i);
                Double matID = row.getCell(1).getNumericCellValue();
                double[] properties = new double[5];
                for (int j = 2; j <= 6; j++) {
                    properties[j - 2] = row.getCell(j).getNumericCellValue();
                }
                metalMap.put(matID.toString(), properties);
            }

            // 读取铺层属性
            for (int i = 1; i <= skinPropertiesSheet.getLastRowNum(); i++) {
                Row row = skinPropertiesSheet.getRow(i);
                String plyID = row.getCell(0).getStringCellValue();

                if (plyID.contains("N")) {
                    // 处理蜂窝层
                    Row resultRow = resultSheet.createRow(i);
                    resultRow.createCell(0).setCellValue(plyID);
                    resultRow.createCell(5).setCellValue(
                        Double.parseDouble(plyID.substring(1)) +
                            (int)(row.getCell(1).getNumericCellValue() / 10000) * 100000
                    );
                } else if (plyID.contains("M")) {
                    // 处理多层角片
                    double thickness = 0;
                    double Exx_eqv = 0;
                    double Eyy_eqv = 0;
                    double Gxy_eqv = 0;
                    double rho = 0;
                    double alp1 = 0;
                    double alp2 = 0;

                    // 遍历每个层并进行计算
                    for (int j = 2; j <= row.getLastCellNum() - 1; j++) {
                        String matID = row.getCell(j).getStringCellValue();
                        double plyThickness = row.getCell(j + 1).getNumericCellValue();

                        if (matMap.containsKey(matID)) {
                            double[] properties = matMap.get(matID);

                            // 计算等效模量和其他参数
                            double Exx = properties[0];
                            double Eyy = properties[1];
                            double Gxy = properties[2];
                            double plyRho = properties[3];
                            double plyAlp1 = properties[4];
                            double plyAlp2 = properties[5];

                            Exx_eqv += Exx * plyThickness;
                            Eyy_eqv += Eyy * plyThickness;
                            Gxy_eqv += Gxy * plyThickness;
                            rho += plyRho * plyThickness;
                            alp1 += plyAlp1 * plyThickness;
                            alp2 += plyAlp2 * plyThickness;
                            thickness += plyThickness;
                        } else if (metalMap.containsKey(matID)) {
                            double[] properties = metalMap.get(matID);

                            // 计算等效模量和其他参数
                            double Exx = properties[0];
                            double Eyy = properties[1];
                            double Gxy = properties[2];
                            double plyRho = properties[3];
                            double plyAlp1 = properties[4];
                            double plyAlp2 = properties[5];

                            Exx_eqv += Exx * plyThickness;
                            Eyy_eqv += Eyy * plyThickness;
                            Gxy_eqv += Gxy * plyThickness;
                            rho += plyRho * plyThickness;
                            alp1 += plyAlp1 * plyThickness;
                            alp2 += plyAlp2 * plyThickness;
                            thickness += plyThickness;
                        }
                    }

                    // 最终计算等效参数
                    Exx_eqv /= thickness;
                    Eyy_eqv /= thickness;
                    Gxy_eqv /= thickness;
                    rho /= thickness;
                    alp1 /= thickness;
                    alp2 /= thickness;

                    // 将结果写入结果表
                    Row resultRow = resultSheet.createRow(i);
                    resultRow.createCell(0).setCellValue(plyID);
                    resultRow.createCell(1).setCellValue(Exx_eqv);
                    resultRow.createCell(2).setCellValue(Eyy_eqv);
                    resultRow.createCell(3).setCellValue(Gxy_eqv);
                    resultRow.createCell(4).setCellValue(thickness);
                    resultRow.createCell(5).setCellValue("多层角片");
                    resultRow.createCell(6).setCellValue(rho);
                    resultRow.createCell(7).setCellValue(alp1);
                    resultRow.createCell(8).setCellValue(alp2);
                } else {
                    // 处理其他类型铺层
                    // 假设其他类型铺层的处理方式不同，这里省略详细代码
                    // 可以根据实际需求补充逻辑
                }
            }

            // 保存计算结果
            FileOutputStream outputFile = new FileOutputStream("CR929_Layup_Lib_Updated.xlsx");
            layupLibWorkbook.write(outputFile);
            outputFile.close();

            matLibFile.close();
            layupLibFile.close();
            matLibWorkbook.close();
            layupLibWorkbook.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
